//
//  PickerTimeView.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 16/5/28.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "PickerTimeView.h"
#import "Masonry.h"
#import "EducationAdminClient_URL.h"

@implementation PickerTimeView{
    UIView              *withShowPicView;
    NSString            *dateStr;

}

-(instancetype)init{

    self = [super init];
    if (self) {
        [self createUI];
    }
    
    return self;
}

-(void)createUI{

    UIImageView *ingView = [[UIImageView alloc]init];
    [self addSubview:ingView];
    [ingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(self.mas_height);
    }];
    
    ingView.backgroundColor = [UIColor blackColor];
    ingView.alpha = 0.5;
    
    withShowPicView = [[UIView alloc]init];
    [self addSubview:withShowPicView];
    withShowPicView.backgroundColor = [UIColor whiteColor];
    [withShowPicView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.equalTo(@(342/2));
    }];
    
    
    
    UIImageView *whichView = [[UIImageView alloc]init];
    whichView.image = [UIImage imageNamed:@"whitchB"];
    whichView.userInteractionEnabled = YES;
    [withShowPicView addSubview:whichView];
    [whichView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(withShowPicView.mas_top);
        make.left.equalTo(withShowPicView.mas_left);
        make.right.equalTo(withShowPicView.mas_right);
        make.height.equalTo(@(100/2));
    }];
    UIButton *cancelButton = [[UIButton alloc]init];
    //    cancelButton.backgroundColor = [UIColor blackColor];
    [whichView addSubview:cancelButton];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(whichView.mas_centerY);
        make.left.equalTo(whichView.mas_left).offset(32/2);
        //        make.right.equalTo(withShowPicView.mas_right);
        make.height.equalTo(@(48/2));
    }];
    
    //这里添加功能判断
    if (getEnglishLanguage()) {
        [cancelButton setImage:[UIImage imageNamed:@"cancelNbUTTONE"] forState:UIControlStateNormal];
        [cancelButton setImage:[UIImage imageNamed:@"cancelButtonE"] forState:UIControlStateSelected];
    }else{
        [cancelButton setImage:[UIImage imageNamed:@"cancelNbUTTON"] forState:UIControlStateNormal];
        [cancelButton setImage:[UIImage imageNamed:@"cancelButton"] forState:UIControlStateSelected];
    }
    UILabel *centerLabel = [[UILabel alloc]init];
    [whichView addSubview:centerLabel];
    [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cancelButton.mas_centerY);
        make.centerX.equalTo(whichView.mas_centerX);
        //        make.right.equalTo(withShowPicView.mas_right);
        //        make.height.equalTo(@(48/2));
    }];
    
    
    centerLabel.font = [UIFont systemFontOfSize:32/2];
    //这里添加功能判断
    if (getEnglishLanguage()) {
        centerLabel.text = @"Select Time";
    }else{
        centerLabel.text = @"选择时间";

    }
    centerLabel.textColor = [UIColor colorWithRed:0x74/255.0 green:0x75/255.0 blue:0x79/255.0 alpha:1];
    UIButton *okButton = [[UIButton alloc]init];
    [whichView addSubview:okButton];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cancelButton.mas_centerY);
        make.right.equalTo(whichView.mas_right).offset(-32/2);
        //        make.right.equalTo(withShowPicView.mas_right);
        make.height.equalTo(@(48/2));
    }];
    [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
    [okButton addTarget:self action:@selector(okClick) forControlEvents:UIControlEventTouchUpInside];
    //这里添加功能判断
    if (getEnglishLanguage()) {
        [okButton setImage:[UIImage imageNamed:@"okNButtonE"] forState:UIControlStateNormal];
        [okButton setImage:[UIImage imageNamed:@"okButotnE"] forState:UIControlStateSelected];
    }else{
        [okButton setImage:[UIImage imageNamed:@"okNButton"] forState:UIControlStateNormal];
        [okButton setImage:[UIImage imageNamed:@"okButotn"] forState:UIControlStateSelected];
    }
    whichView.backgroundColor = [UIColor grayColor];
    //    cancelButton.backgroundColor = [UIColor blackColor];
    //    okButton.backgroundColor = [UIColor orangeColor];
    
   UIDatePicker *datePicer = [[UIDatePicker alloc]init];
    

    _datePicer = datePicer;
    //这里添加功能判断
    if (getEnglishLanguage()) {
        [_datePicer setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
    }else{
        
    }
    _datePicer.backgroundColor = [UIColor whiteColor];
    
    [_datePicer setTimeZone:[NSTimeZone systemTimeZone]];
    [_datePicer setDate:[NSDate date]];
    [_datePicer addTarget:self action:@selector(dateClick:) forControlEvents:UIControlEventValueChanged];
    
//    //这里添加功能判断
//    if (getEnglishLanguage()) {
//        [_datePicer setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]];
//    }else{
//        
//    }
    [withShowPicView addSubview:_datePicer];
    [_datePicer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(withShowPicView.mas_centerX);
        make.bottom.equalTo(withShowPicView.mas_bottom);
        make.top.equalTo(whichView.mas_bottom);
        make.width.equalTo(withShowPicView.mas_width);
    }];

}


-(void)dateClick:(UIDatePicker *)datePic{
    NSDate *date = [[NSDate alloc]initWithTimeInterval:0 sinceDate:datePic.date];
    
    //NSDate *date = [[NSDate alloc]initWithTimeInterval:8*60*60 sinceDate:datePic.date];

    NSString *str = [NSString stringWithFormat:@"%@", date];
    
    NSArray *strArr = [str componentsSeparatedByString:@" "];
    NSArray *minArr = [strArr[1] componentsSeparatedByString:@":"];
    NSString *lastS = [NSString stringWithFormat:@"%@:%@", minArr[0], minArr[1]];
    dateStr = lastS;
    NSLog(@"%@", str);
}
-(void)cancelClick{
    dateStr = @"";
    self.hidden = YES;
}

-(void)okClick{
    
    
    NSDate *date = [[NSDate alloc]initWithTimeInterval:0 sinceDate:_datePicer.date];
    
    //NSDate *date = [[NSDate alloc]initWithTimeInterval:8*60*60 sinceDate:_datePicer.date];

    //NSString *str = [NSString stringWithFormat:@"%@", date];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    if(_datePicer.datePickerMode == UIDatePickerModeDate){
    
    }else if(_datePicer.datePickerMode == UIDatePickerModeTime){
        [dateFormatter setDateFormat:TIMEFORMATE];

    }else if(_datePicer.datePickerMode == UIDatePickerModeDateAndTime){
        [dateFormatter setDateFormat:DATEFORMATE];

    }
    
    
    if(self.dataFormate){
        [dateFormatter setDateFormat:self.dataFormate];
    }

    NSString *setTime=[dateFormatter stringFromDate:date];
//        NSArray *strArr = [str componentsSeparatedByString:@" "];
    //    NSArray *minArr = [strArr[1] componentsSeparatedByString:@":"];
    NSString *lastStr = setTime; // [NSString stringWithFormat:@"%@:%@", minArr[0], minArr[1]];
    dateStr = lastStr;

    if(self.delegagte)[self.delegagte performSelector:@selector(picTimeReceive:) withObject:dateStr];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
